version 19
drop _all

* change the root folder according to your computer directory
global root = "D:\WorkingPaper-Series\Township_Light\Replication_Files"  
     
global dofiles = "$root\dofile"     
global working_data = "$root\workdata"
global tables = "$root\table"
global figures = "$root\figure"

cd "$working_data"


******************
*****Pre Set******
******************

global geo_ctrl = "topography_t* slope_t* roughness_t*"
global wth_ctrl = "gaez_t* precipitation_t* sunlight_t*"
global dist_ctrl = "county_dist_t* dist_hz_t* dist_coast_t*"

global full_ctrl = "$geo_ctrl $wth_ctrl $dist_ctrl"

global geo_ctrl_1 = "topography_t_1 slope_t_1 roughness_t_1"
global wth_ctrl_1 = "gaez_t_1 precipitation_t_1 sunlight_t_1"
global dist_ctrl_1 = "county_dist_t_1 dist_hz_t_1 dist_coast_t_1"

global full_ctrl_1 = "$geo_ctrl_1 $wth_ctrl_1 $dist_ctrl_1"

global geo_ctrl_2 = "topography_t_2 slope_t_2 roughness_t_2"
global wth_ctrl_2 = "gaez_t_2 precipitation_t_2 sunlight_t_2"
global dist_ctrl_2 = "county_dist_t_2 dist_hz_t_2 dist_coast_t_2"

global full_ctrl_2 = "$geo_ctrl_2 $wth_ctrl_2 $dist_ctrl_2"

global matchvar = "county_dist topography slope gaez sunlight dist_hz dist_coast"


global pre_ctrl = "light_t road_t pop_t"


*******************
***** Table 1 *****
*******************

use "township_panel_final.dta", clear

global town_statics "log_light N_village village treat log_road log_firm_entry topography slope roughness gaez precipitation sunlight county_dist dist_hz dist_coast"
logout,save("$tables\table1") word replace: fsum $town_statics, s(N mean sd)

*******************
***** Table 2 *****
*******************

use "township_panel_final.dta", clear

global outtable = "$tables/table2.rtf"
local outreg_opt = "nor nocons dec(3)"

* column (1)
reghdfe log_light village treat_trend, a(county_year idcode) cl(countycode)
qui sum log_light
scalar beta_dot = e(b)[1,1]
scalar R_dot = e(r2_within)
outreg2 using $outtable, keep(village) `outreg_opt' replace

* column (2)
reghdfe log_light village treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
qui sum log_light
scalar beta_tilde = e(b)[1,1]
scalar R_tilde = e(r2_within)
outreg2 using $outtable, keep(village) `outreg_opt' append
***** Oster Delta
scalar delta_3 = beta_tilde*(R_tilde-R_dot)/(0.3*R_tilde*(beta_dot-beta_tilde))

* column (3)
**********IPW Weighting************
use "township_panel_final.dta", clear

logit treat $matchvar
predict pw
replace pw = 1-pw if treat==0
sum pw,d 
keep if pw > `r(p1)' & pw < `r(p99)'
gen ipw = 1/pw

reghdfe log_light village treat_trend $full_ctrl [pweight = ipw], a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village) `outreg_opt' append
***********************************

use "township_panel_final.dta", clear

* column (4)
******** Coarsened Exact Matching ********
cem topography (#10) slope (#10) roughness (#10) gaez (#10) precipitation (#10) sunlight (#10) county_dist (#10) dist_hz (#10) dist_coast (#10) ,tr(treat)
reghdfe log_light village treat_trend $full_ctrl [aw = cem_weights] if cem_matched == 1, a(county_year idcode) cl(countycode)
qui sum log_light if cem_matched == 1
outreg2 using $outtable, keep(village) `outreg_opt' append

****************************************

* column (5)
reghdfe log_light village $pre_ctrl $full_ctrl, a(county_year idcode) cl(countycode)
outreg2 using $outtable, keep(village) `outreg_opt' append

* column (6)
reghdfe ihs_light village treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
qui sum ihs_light
outreg2 using $outtable, keep(village) `outreg_opt' append
***** Oster Delta
scalar beta_tilde = e(b)[1,1]
scalar R_tilde = e(r2_within)
qui reghdfe ihs_light village treat_trend, a(county_year idcode) cl(countycode)
scalar beta_dot = e(b)[1,1]
scalar R_dot = e(r2_within)
scalar delta_ihs = beta_tilde*(R_tilde-R_dot)/(0.3*R_tilde*(beta_dot-beta_tilde))
*****

* column (7)
reghdfe ihs_light village $pre_ctrl $full_ctrl, a(county_year idcode) cl(countycode)
outreg2 using $outtable, keep(village) `outreg_opt' append


*******************
***** Table 3 *****
*******************

use "township_panel_final.dta", clear

global outtable = "$tables/table3.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes)"'
local outreg_opt = "nor nocons dec(3)"
local apd_opt = "replace"

foreach w in digit_village rural_revital total_freq{
	qui sum `w',d 
	gen vil_`w'_mean = village*(`w'>`r(mean)')
	gen treat_`w'_mean = treat*(`w'>`r(mean)')
	foreach y in log_light ihs_light {
		qui reghdfe `y' vil_`w'_mean village treat_`w'_mean $full_ctrl treat_trend, a(county_year idcode) cl(countycode)
		qui sum `y'
		outreg2 using $outtable, keep(village vil_`w'_mean)  `addstat' `addtext' `outreg_opt' `apd_opt'
		local apd_opt = "append"
	}
}

*******************
***** Table 4 *****
*******************

use "township_panel_final.dta", clear

global outtable = "$tables/table4.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes)"'
local outreg_opt = "nor nocons dec(3)"
local apd_opt = "replace"

reghdfe log_light village_mention village treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village_mention village)  `addstat' `addtext' `outreg_opt' `apd_opt'
local apd_opt = "append"
reghdfe ihs_light village_mention village treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
qui sum ihs_light
outreg2 using $outtable, keep(village_mention village)  `addstat' `addtext' `outreg_opt' `apd_opt'

reghdfe log_light village_policy village village_treat treat_policy treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
qui sum log_light
outreg2 using $outtable, keep(village_policy village)  `addstat' `addtext' `outreg_opt' `apd_opt'
reghdfe ihs_light village_policy village village_treat treat_policy treat_trend $full_ctrl, a(county_year idcode) cl(countycode)
qui sum ihs_light
outreg2 using $outtable, keep(village_policy village)  `addstat' `addtext' `outreg_opt' `apd_opt'



*******************
***** Table 5 *****
*******************

use "township_panel_final.dta", clear

global outtable = "$tables/table5.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes)"'
local outreg_opt = "nor nocons dec(3)"
local apd_opt = "replace"

foreach w in CS_term_2014 CS_rotation_2014 CM_term_2014 CM_rotation_2014 {
	qui gen `w'_village = village*`w'
	foreach y in log_light ihs_light {
		qui reghdfe `y' `w'_village village $full_ctrl treat_trend , a(county_year idcode) cl(countycode)
		qui sum `y' if `w'!=.
		outreg2 using $outtable, keep(`w'_village village)  `addstat' `addtext' `outreg_opt' `apd_opt'
		local apd_opt = "append"
	}
}


*******************
***** Table 6 *****
*******************

use "township_panel_final.dta", clear

global outtable = "$tables/table6.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes)"'
local outreg_opt = "nor nocons dec(3)"
local apd_opt = "replace"

foreach cond in "(first_year>=0)" "(first_year>=2016|first_year==0)" "(first_year<2016)" {
	qui reghdfe road_ipol pre_village village treat_trend if `cond', a(county_year idcode) cl(countycode)
	qui sum road_ipol if `cond'
	outreg2 using $outtable, keep(pre_village village)  `addstat' `addtext' `outreg_opt' `apd_opt'
	local apd_opt = "append"
	qui reghdfe road_ipol pre_village village treat_trend $full_ctrl if `cond', a(county_year idcode) cl(countycode)
	qui sum road_ipol if `cond'
	outreg2 using $outtable, keep(pre_village village)  `addstat' `addtext' `outreg_opt' `apd_opt'
	local apd_opt = "append"
}

*******************
***** Table 7 *****
*******************

use "township_panel_final.dta", clear

global outtable = "$tables/table7.rtf"
local addstat = `"addstat("Dep. Var. Mean", r(mean), "Number of Clusters", e(N_clust), "Adjusted R-squared", e(r2_a))"'
local addtext = `"addtext("Township FE",Yes, "County by Year FE",Yes, "Treatment Linear Trend",Yes)"'
local outreg_opt = "nor nocons dec(3)"
local apd_opt = "replace"

foreach cond in "(first_year>=0)" "(first_year>=2016|first_year==0)" "(first_year<2016)" {
	qui reghdfe log_firm_entry pre_village village treat_trend if `cond', a(county_year idcode) cl(countycode)
	qui sum log_firm_entry if `cond'
	outreg2 using $outtable, keep(pre_village village)  `addstat' `addtext' `outreg_opt' `apd_opt'
	local apd_opt = "append"
	qui reghdfe log_firm_entry pre_village village treat_trend $full_ctrl if `cond', a(county_year idcode) cl(countycode)
	qui sum log_firm_entry if `cond'
	outreg2 using $outtable, keep(pre_village village)  `addstat' `addtext' `outreg_opt' `apd_opt'
	local apd_opt = "append"
}
